package roudao

import (
	"wzgames/internal/gdao"
	"wzgames/internal/model/roulettep"
)

type RouletteDao gdao.DaoSource

const sqlGetTopRounds = "select * from roulette_rounds where `instid`=? order by `rid` desc limit 10"

func (dao *RouletteDao) GetTopRounds(instId int64) ([]*roulettep.RouletteRoundEntity, error) {
	rows, err := dao.Queryx(sqlGetTopRounds, instId)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return nil, err
	}
	var rounds []*roulettep.RouletteRoundEntity
	for rows.Next() {
		rd := &roulettep.RouletteRoundEntity{}
		err = rows.StructScan(rd)
		if err != nil {
			//logger.Error("err == ", zap.String("error", err.Error()))
			return nil, err
		}
		rounds = append(rounds, rd)
	}
	return rounds, err
}

const sqlGetRouRound = "select * from roulette_rounds where `rid`=? and `instid`=? limit 1"

func (dao *RouletteDao) GetRouRound(roundId, instId int64) (*roulettep.RouletteRoundEntity, error) {
	r := &roulettep.RouletteRoundEntity{}
	err := dao.Get(r, sqlGetRouRound, roundId, instId)
	return r, err
}

const sqlCreateRound = "insert into roulette_rounds(`rid`,`instid`,`start`,`end`,`sseed`,`sseed256`,`clientseeds`,`h512`,`num`,`color`,`grp`,`status`,`count`,`op`,`created`,`updated`)" +
	" values(:rid,:instid,:start,:end,:sseed,:sseed256,:clientseeds,:h512,:num,:color,:grp,:status,:count,:op,:created,:updated)"

func (dao *RouletteDao) CreateRound(rd *roulettep.RouletteRoundEntity) error {
	_, err := dao.NamedExec(sqlCreateRound, rd)
	return err
}

const sqlChangeState2Bet = "update roulette_rounds set `start`=:start,`status`=:status,`updated`=:updated where `rid`=:rid and `instid`=:instid"

func (dao *RouletteDao) ChangeToBetState(rd *roulettep.RouletteRoundEntity) (int64, error) {
	result, err := dao.NamedExec(sqlChangeState2Bet, rd)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlChangeState2StopBet = "update roulette_rounds set `status`=:status,`updated`=:updated where `rid`=:rid and `instid`=:instid"

func (dao *RouletteDao) ChangeToStopBetState(rd *roulettep.RouletteRoundEntity) (int64, error) {
	result, err := dao.NamedExec(sqlChangeState2StopBet, rd)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlChangeState2Open = "update roulette_rounds set `op`=:op,`end`=:end,`clientseeds`=:clientseeds,`h512`=:h512,`num`=:num,`color`=:color,`grp`=:grp,`status`=:status,`count`=:count,`updated`=:updated where `rid`=:rid and `instid`=:instid"

func (dao *RouletteDao) ChangeToOpenState(rd *roulettep.RouletteRoundEntity) (int64, error) {
	result, err := dao.NamedExec(sqlChangeState2Open, rd)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlCreateRouNodeRound = "insert into roubig_node_rounds(`nodeid`,`instid`,`roundid`,`status`,`result`,`dateline`) values(:nodeid,:instid,:roundid,:status,:result,:dateline) on duplicate key update status=values(status),dateline=values(dateline)"

func (dao *RouletteDao) CreateRouNodeRound(data *roulettep.RouNodeRoundEntity) error {
	_, err := dao.NamedExec(sqlCreateRouNodeRound, data)
	return err
}

const sqlGetRouNodeRound = "select * from roubig_node_rounds where `nodeid`=? and `instid`=? and `roundid`=? limit 1"

func (dao *RouletteDao) GetRouNodeRound(nodeId, instId, roundId int64) (*roulettep.RouNodeRoundEntity, error) {
	r := &roulettep.RouNodeRoundEntity{}
	err := dao.Get(r, sqlGetRouNodeRound, nodeId, instId, roundId)
	return r, err
}
